const vscode = require('vscode');
const path = require("path");


async function createTemplate (uri, fileNmae) {

  const folderUri = vscode.workspace.workspaceFolders[0].uri;

  /**
   * 读取模板
   */
  const templateurl = path.join(__filename, '..', '..', 'template', fileNmae);
  const templateUri = folderUri.with({ path: path.posix.join(templateurl) });
  const readData = await vscode.workspace.fs.readFile(templateUri);
  const writeData = Buffer.from(readData);

  /**
   * 在当前选中路径下创建模板
   */
  let newFile = uri.path.split("/");
  newFile[newFile.length - 1] = fileNmae;
  newFile = newFile.join("/");
  const fileUri = folderUri.with({ path: path.posix.join(newFile) });
  await vscode.workspace.fs.writeFile(fileUri, writeData);

}


module.exports = function (context) {

  /**创建二级菜单页面模板 */
  context.subscriptions.push(vscode.commands.registerCommand('extension.createSecondTemplate', async (uri) => {

    createTemplate(uri, "createMenuTemplate.vue");

  }));


  /**创建外链模板 */
  context.subscriptions.push(vscode.commands.registerCommand('extension.createOutsideTemplate', async (uri) => {

    createTemplate(uri, "createOutsideTemplate.vue");

  }));

};